package com.example.communitysystem.controller;

import com.example.communitysystem.dto.PaginationDTO;
import com.example.communitysystem.mapper.UserMapper;
import com.example.communitysystem.model.User;
import com.example.communitysystem.service.NotificationService;
import com.example.communitysystem.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ProfileController {

    private UserMapper userMapper;

    private QuestionService questionService;

    private NotificationService notificationService;

    @GetMapping("/profile/{action}")
    public String profile(
            @PathVariable(name = "action")String action,
            Model model,
            HttpServletRequest request,
            @RequestParam(name = "page", defaultValue = "1")Integer page,
            @RequestParam(name = "size", defaultValue = "5")Integer size
    ){
        User user = null;
        user = (User)request.getSession().getAttribute("user");
        if(user == null){
            return "redirect:/";
        }

        if("question".equals(action)){
            model.addAttribute("section","question");
            model.addAttribute("sectionName","我的提问");
            //获取当前用户的问题信息，添加到Model对象中
            PaginationDTO paginationDTO = questionService.list(user.getId(),page,size);
            model.addAttribute("paginationDTO",paginationDTO);
        }else if("reply".equals(action)){
            model.addAttribute("section","reply");
            model.addAttribute("sectionName","最新回复");
            PaginationDTO paginationDTO = notificationService.list(user.getId(),page,size);
            model.addAttribute("paginationDTO",paginationDTO);
        }
        //获取当前用户的未读消息数
        Integer unreadCount = notificationService.getUnreadCount(user.getId());
        model.addAttribute("unreadCount",unreadCount);

        return "profile";
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    @Autowired
    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }

    @Autowired
    public void setNotificationService(NotificationService notificationService) {
        this.notificationService = notificationService;
    }
}
